<template>
  <div class="">
    <div class="">
      <SearchComp :searchForm="searchForm" :searchData="queryString" :searchAction="searchAction"></SearchComp>
    </div>
    <div class="m-t">
      <div class="btnStyle flex-a-c  ">
        <el-button type="primary" size="small" @click="add">新增</el-button>
        <el-upload ref="uploadRefXls" class="upload-demo" action="#" :limit="1" :on-change="handleChangeXls" :auto-upload="false"
          :show-file-list="false" :on-exceed="handleExceedXls" accept=".xls,.xlsx">
          <template #trigger>
            <el-button type="primary" size="small">批量导入</el-button>
          </template>
        </el-upload>
        <el-button type="primary" size="small" @click="download">下载模板</el-button>
        <el-button type="danger" size="small" @click="dels">批量删除</el-button>
      </div>
      <TableCompEle class="m-t" ref="multipleTable" height="50vh" :tableData="tableData" :tableConfig="tableConfig" :tableAction="tableAction"
        :isSelect="true" :isIndex="true" :isShowPage="true" :queryParams="queryParams" @getPagination="getList"
        @handleSelectionChange="handleSelectionChange"></TableCompEle>
    </div>
    <Add ref="addRef" @close="getList" />
    <Comment ref="commentRef" @closeComment="getList" />
  </div>
</template>

<script>
import {
  // getArticleList,
  batchDelete,
  downloadTemplate,
  importRegulations
} from '@/api/publishManage'
import Add from './add.vue'
import Comment from './comment.vue'
export default {
  components: { Add, Comment },
  data() {
    return {
      flag: 3,
      multipleSelection: [],
      queryParams: {
        total: 0,
        currentPage: 1,
        pageSize: 10
      },
      tableData: [],
      tableConfig: [
        {
          prop: 'title',
          label: '标题'
        },
        {
          prop: 'source',
          label: '来源'
        },
        {
          prop: 'commentCount',
          label: '评论数',
          width: 120
        }
      ],
      tableAction: {
        align: () => 'center',
        width: () => String(180),
        operations: [
          {
            label: '评论',
            isLink: true,
            type: 'primary',
            method: row => {
              this.$refs.commentRef.show({
                title: '评论政策法规',
                flag: this.flag,
                data: row
              })
            }
          },
          {
            label: '查看',
            isLink: true,
            type: 'primary',
            method: row => {
              this.$refs.addRef.show({
                title: '查看政策法规',
                isLook: true,
                flag: this.flag,
                data: row
              })
            }
          },
          {
            label: '修改',
            isLink: true,
            type: 'primary',
            method: row => {
              this.$refs.addRef.show({
                title: '修改政策法规',
                isLook: false,
                flag: this.flag,
                data: row
              })
            }
          },
          {
            label: '删除',
            isLink: true,
            type: 'danger',
            method: row => {
              this.deleteFun([row.articleId])
            }
          }
        ]
      },
      queryString: {
        title: '',
        source: ''
      },
      searchForm: {
        changeMethod: () => {
          this.queryParams.currentPage = 1
          this.getList()
        },
        formData: [
          {
            type: 'Input',
            label: '标题',
            prop: 'title',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入标题'
          },
          {
            type: 'Input',
            label: '来源',
            prop: 'source',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入来源'
          }
        ]
      },
      searchAction: [
        {
          label: '查询',
          type: 'primary',
          handle: () => {
            this.queryParams.currentPage = 1
            this.$refs.multipleTable.clearSelection()
            this.getList()
          }
        },
        {
          label: '重置',
          type: 'primary',
          plain: true,
          handle: () => {
            this.queryString = {
              title: '',
              source: ''
            }
            this.queryParams.currentPage = 1
            this.getList()
          }
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  mounted() { },
  methods: {
    add() {
      this.$refs.addRef.show({
        title: '新增政策法规',
        isLook: false,
        flag: this.flag
      })
    },
    download() {
      downloadTemplate()
    },
    dels() {
      if (this.multipleSelection.length == 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的数据'
        })
        return
      }
      const ids = []
      this.multipleSelection.forEach(items => {
        ids.push(items.articleId)
      })
      this.deleteFun(ids)
    },
    getList() {
      let res = {
        "code": 200,
        "message": "操作成功",
        "data": {
          "pageSize": 10,
          "totalPage": 1,
          "total": 8,
          "list": [
            {
              "articleId": 145,
              "title": "国务院关于印发《推动大规模设备更新和 消费品以旧换新行动方案》的通知",
              "source": "中国政府网",
              "content": "为贯彻落实《中华人民共和国国民经济和社会发展第十四个五年规划和2035年远景目标纲要》和《中共中央 国务院关于构建更加完善的要素市场化配置体制机制的意见》，建设高标准技术要素市场，科技部编制了《“十四五”技术要素市场专项规划》。现印发给你们，请结合实际情况认真贯彻实施。",
              "state": "已发布",
              "fileId": 886,
              "deleteFlag": "n",
              "createTime": [
                2023,
                6,
                13,
                15,
                18,
                27
              ],
              "createBy": "106",
              "modifyTime": [
                2023,
                6,
                13,
                15,
                18,
                27
              ],
              "modifyBy": "106",
              "version": "V0.1",
              "flag": 1,
              "publishTime": "2023-06-13 15:18:31",
              "commentCount": 1
            },
            {
              "articleId": 144,
              "title": "工业和信息化部 国家发展改革委 国务院国资委关于支持首台（套）重大技术装备平等参与企业招标投标活动的指导意见",
              "source": "中国政府网",
              "content": "现将《关于加强高水平科技成果转移转化人才队伍建设的若干措施》印发给你们，请结合实际认真贯彻落实。",
              "state": "已发布",
              "fileId": 885,
              "deleteFlag": "n",
              "createTime": [
                2023,
                6,
                13,
                15,
                17,
                12
              ],
              "createBy": "106",
              "modifyTime": [
                2023,
                6,
                13,
                15,
                17,
                28
              ],
              "modifyBy": "106",
              "version": "V1,0",
              "flag": 1,
              "publishTime": "2023-06-13 15:17:17",
              "commentCount": 0
            },
            {
              "articleId": 143,
              "title": "关于研发机构采购设备增值税政策的公告",
              "source": "中国政府网",
              "content": "为贯彻落实党中央、国务院关于加强数字政府建设的决策部署，持续提高专利审查服务信息化和便利化水平，国家知识产权局自2023年2月7日（含当日）起，全面推行专利证书电子化。当事人以电子形式申请并获得专利授权的，通过专利业务办理系统下载电子专利证书；以纸质形式申请并获得专利授权的，按照《领取电子专利证书通知书》中告知的方式下载电子专利证书。",
              "state": "已发布",
              "fileId": 884,
              "deleteFlag": "n",
              "createTime": [
                2023,
                6,
                13,
                15,
                15,
                9
              ],
              "createBy": "106",
              "modifyTime": [
                2023,
                6,
                13,
                15,
                15,
                9
              ],
              "modifyBy": "106",
              "version": "V1.0",
              "flag": 1,
              "publishTime": "2023-06-13 15:15:21",
              "commentCount": 0
            },
            {
              "articleId": 142,
              "title": "国家重大科研基础设施和大型科研仪器开放共享评价考核实施细则",
              "source": "中国政府网",
              "content": "各省、自治区、直辖市和新疆生产建设兵团知识产权局，四川省知识产权服务促进中心，各地方有关中心：\n\n为贯彻落实中共中央、国务院印发的《知识产权强国建设纲要（2021—2035年）》和国务院印发的《“十四五”国家知识产权保护和运用规划》，加强知识产权维权援助工作指导，完善工作流程，强化规范化管理，经广泛征求地方意见，制定形成《知识产权维权援助工作指引》，现印发给你们，请在工作中结合实际参照推行，进一步",
              "state": "已发布",
              "fileId": 883,
              "deleteFlag": "n",
              "createTime": [
                2023,
                6,
                13,
                15,
                12,
                3
              ],
              "createBy": "106",
              "modifyTime": [
                2023,
                6,
                13,
                15,
                13,
                57
              ],
              "modifyBy": "106",
              "version": "1.0",
              "flag": 1,
              "publishTime": "2023-06-13 15:12:21",
              "commentCount": 0
            },
            {
              "articleId": 130,
              "title": "《产学研合作协议知识产权相关条款制定指引（试行）》解读",
              "source": "中国政府网",
              "content": "要严格遵守单位制定的管理制度。",
              "state": "已发布",
              "fileId": 825,
              "deleteFlag": "n",
              "createTime": [
                2023,
                6,
                1,
                9,
                51,
                19
              ],
              "createBy": "1261",
              "modifyTime": [
                2023,
                6,
                1,
                9,
                51,
                19
              ],
              "modifyBy": "1261",
              "version": "V1.0",
              "flag": 1,
              "publishTime": "2023-06-01 09:54:33",
              "commentCount": 0
            },
            {
              "articleId": 129,
              "title": "产学研合作协议知识产权相关条款制定指引（试行）",
              "source": "中国政府网",
              "content": "所有的经费收入支出必须符合单位规定的审批标准方可审批通过。",
              "state": "已发布",
              "fileId": 824,
              "deleteFlag": "n",
              "createTime": [
                2023,
                6,
                1,
                9,
                50,
                15
              ],
              "createBy": "1261",
              "modifyTime": [
                2023,
                6,
                1,
                9,
                50,
                15
              ],
              "modifyBy": "1261",
              "version": "V1.0",
              "flag": 1,
              "publishTime": "2023-06-01 09:54:34",
              "commentCount": 0
            },
            {
              "articleId": 128,
              "title": "科研院所等科研机构免税进口科学研究、科技开发和教学用品管理细则",
              "source": "中国政府网",
              "content": "按照单位规定的方案进行实施运行",
              "state": "已发布",
              "fileId": 823,
              "deleteFlag": "n",
              "createTime": [
                2023,
                6,
                1,
                9,
                48,
                35
              ],
              "createBy": "1261",
              "modifyTime": [
                2023,
                6,
                1,
                9,
                48,
                35
              ],
              "modifyBy": "1261",
              "version": "V0.8",
              "flag": 1,
              "publishTime": "2023-06-01 09:54:37",
              "commentCount": 0
            },
            {
              "articleId": 127,
              "title": "科技部成果转化与区域创新司 教育部科学技术与信息化司关于首批高校专业化国家技术转移机构建设试点启动的通知",
              "source": "中国政府网",
              "content": "必须符合本单位规定的审核标准方可审核通过",
              "state": "已发布",
              "fileId": 822,
              "deleteFlag": "n",
              "createTime": [
                2023,
                6,
                1,
                9,
                47,
                47
              ],
              "createBy": "1261",
              "modifyTime": [
                2023,
                6,
                1,
                9,
                47,
                47
              ],
              "modifyBy": "1261",
              "version": "V1.0",
              "flag": 1,
              "publishTime": "2023-06-01 09:54:39",
              "commentCount": 0
            }
          ],
          "page": 1
        }
      }
      // getArticleList(
      //   Object.assign(
      //     {
      //       currentPage: this.queryParams.currentPage,
      //       pageSize: this.queryParams.pageSize
      //     },
      //     this.queryString,
      //     { flag: this.flag }
      //   )
      // ).then(res => {
      this.queryParams.total = res?.data?.total ?? 0
      this.tableData = res?.data?.list ?? []
      // })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteFun(ids) {
      this.$confirm(`确定要删除吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          batchDelete({
            ids: ids
          }).then(() => {
            this.$refs.multipleTable.clearSelection()
            this.getList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // xls
    handleChangeXls(file) {
      const _file = file?.raw ?? file
      const formData = new FormData()
      formData.append('file', _file)
      formData.append('pathType', file.name.split('.').pop())
      importRegulations(formData).then(() => {
        this.getList()
      })
    },
    // 多次上传覆盖
    handleExceedXls(file) {
      this.$refs.uploadRefXls.clearFiles()
      this.handleChangeXls(file[0])
    }
  }
}
</script>

<style lang="less" scoped>
  .upload-demo {
    padding: 0 10px;
  }
</style>